package com.buaa.aimall.modelModule.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buaa.aimall.Utils.BaseResponse;
import com.buaa.aimall.Utils.Enum.StatusCode;
import com.buaa.aimall.modelModule.Entity.ModelResourceEntity;
import com.buaa.aimall.modelModule.Service.ModelResourceService;
import com.buaa.aimall.userModule.DTO.PageDTO;
import com.buaa.aimall.userModule.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/model/resource")
@CrossOrigin
public class ModelResourceController {
    @Autowired
    private ModelResourceService modelResourceService;

    @Autowired
    private UserService userService;

    @PostMapping("/get-list")
    public BaseResponse<Page<ModelResourceEntity>> getList(@RequestBody PageDTO request) {
        Page<ModelResourceEntity> page = new Page<>(request.getCurrentPage(), request.getPageSize());
        var user = userService.getCurrentUser();

        LambdaQueryWrapper<ModelResourceEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(ModelResourceEntity::getCreateTime);
        queryWrapper.eq(ModelResourceEntity::getFkOwner, user.getId());

        var entityPage = modelResourceService.page(page, queryWrapper);
        return new BaseResponse<>(StatusCode.SUCCESS, "获取模型资源列表成功", entityPage);
    }
}
